// pages/main/main.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    avatar: '',
    name:'',
    number:'',
    phone:'',
    completedAmounts:'',
    taskAmounts:'',
    transportMileage:''
  },
  onLoad() {
    // 从app.js中拿到初始值
    const app = getApp()
    const { avatar, name, number, phone,completedAmounts, taskAmounts, transportMileage } = app.userInfo
    this.setData({ avatar, name, number, phone,completedAmounts, taskAmounts, transportMileage })
    // 如果有token，就发一次请求，拿到个人信息
    if (app.getToken()) {
      this.getProfile()
      this.getReport()
    }
  },
  async getProfile() {
    const { avatar, name, number, phone } = await wx.$http.get('/driver/users')
    // 更新当前页面数据
    this.setData({ avatar, name, number, phone})

    // 更新下app中的数据
    const app = getApp()
    app.userInfo = { avatar, name, number, phone }
    // console.log(app)
  },
  async getReport() {
    const {completedAmounts, taskAmounts, transportMileage} =await wx.$http.get('/driver/users/taskReport',{year:'2022',month:'07'})
    // const res = await wx.$http.get('/driver/users/taskReport',{year:'2023',month:'09'})
    // console.log(res)
    // 更新当前页面数据
    this.setData({ completedAmounts, taskAmounts, transportMileage })
    // 更新下app中的数据
    const app = getApp()
    app.userInfo = { completedAmounts, taskAmounts, transportMileage}
    console.log(app.userInfo)
  }
})